/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Action;

import Model.Crossing;
import View.CarsCrossingFrame;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Pro
 * Run the main and has message listeners
 */
public class Runcrossing implements MessageListener {

    private Crossing crossing;
    private CarsCrossingFrame carsCrossingFrame;
    private File savedcrossing;

    /**
     *
     * @param H_street
     * @param V_street
     */
    public void setCrossing(int H_street, int V_street) {
        this.crossing = new Crossing(H_street, V_street);
    }

    /**
     *No Argument Constructor
     */
    public Runcrossing() {

        crossing = new Crossing(3, 3);
        carsCrossingFrame = new CarsCrossingFrame(crossing.getLanes());
        carsCrossingFrame.addMessageListener(this);
        carsCrossingFrame.setVisible(true);


    }

    public void encodeObject(Object o, File xmlFile) throws IOException {
        XMLEncoder encoder = new XMLEncoder(
                new FileOutputStream(xmlFile));
        encoder.writeObject(o);
        encoder.close();

    }

    public Object decodeObject(File xmlFile) throws IOException {
        XMLDecoder decoder = new XMLDecoder(
                new FileInputStream(xmlFile));
        Object o = decoder.readObject();
        decoder.close();
        return o;
    }

    /**
     *
     * @throws Exception
     */
//    public void freezeCrossing() throws Exception {
//
//
//
//        savedcrossing = new File("savedcrossing.xml");
//        encodeObject(crossing, savedcrossing);
//        System.out.println("Test saving");
//    }

    /**
     *Passed through from MessageListener for pausing timers in crossing object and display object
     * @param task is a string variable sent from Start Stop button
     */
    public void messageAction(String task) {

        if (task.equals("startStop")) {

            crossing.setPaused(!crossing.isPaused());
        }

        if (task.equals("save")) {

            crossing.setPaused(!crossing.isPaused());
            carsCrossingFrame.setPaused(true);

            try {
                savedcrossing = new File("savedcrossing.xml");
        encodeObject(crossing, savedcrossing);
            } catch (Exception ex) {
                Logger.getLogger(Runcrossing.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        if (task.equals("load")) {

            crossing.setPaused(!crossing.isPaused());
            carsCrossingFrame.setPaused(true);
            try {
                crossing = (Crossing) decodeObject(savedcrossing);
            } catch (IOException ex) {
                Logger.getLogger(Runcrossing.class.getName()).log(Level.SEVERE, null, ex);
            }
            carsCrossingFrame.newLanes(crossing.getLanes());


        }
    }

    /**
     *Passed through from MessageListener for setting of probability  of cars appearing
     * @param task is a string variable for the type lane comparison
     * @param value is Integer for probability comparison
     */
    public void messageAction(String task, int value) {

        if (task.equals("hLanes")) {

            crossing.setCarProb(0, value);
        } else {
            crossing.setCarProb(1, value);
        }
    }

    /**
     *Passed through from MessageListener for construction of new crossing
     * @param task is a string variable for the type lane comparison
     * @param hLanes is Integer for Crossing constructor
     * @param vLanes is Integer for Crossing constructor
     */
    public synchronized void messageAction(String task, int hLanes, int vLanes) {


        if (task.equals("hLanes")) {

            crossing.setPaused(true);
            carsCrossingFrame.setPaused(true);
            crossing = new Crossing(hLanes, vLanes);
            carsCrossingFrame.newLanes(crossing.getLanes());

        }

        if (task.equals("vLanes")) {

            crossing.setPaused(true);
            carsCrossingFrame.setPaused(true);
            crossing = new Crossing(hLanes, vLanes);
            carsCrossingFrame.newLanes(crossing.getLanes());

        }
    }

    /**
     *Main, enough said
     * @param args
     * 
     */
    public static void main(String[] args) {

        Runcrossing runcrossing = new Runcrossing();

//        String Catch = "";
//        int menuSelect = 0;
//        int H_street = 3;
//        int V_street = 3;
//        double chanceCarH_street = 0.5;
//        double chanceCarV_street = 0.5;
//        int totalLanes = 0;
//        boolean pressFive = false;
//        int cycle = 0;
//
//
//        while (true) {
//
//            System.out.println("=== MENU ===\n1. Set Number of horizontal lanes"
//                    + " (H-street) [min 1, max 3]\n2. Set Number of vertical lanes"
//                    + " (V-street) [min 1, max 4]\n3. Set Probability of a car"
//                    + " entering H-street [min 0, max 1]\n4. Set Probability of a"
//                    + " car entering V-street [min 0, max 1]\n5. Run one simulation"
//                    + " cycle\n6. Set and run number of simulation cycles"
//                    + " [min 1, max 10]\n7. Exit\nEnter your choice>");
//
//            Scanner keyboard = new Scanner(System.in);
//            Catch = keyboard.next();
//
//            try {
//                menuSelect = Integer.parseInt(Catch);
//
//            } catch (Exception e) {
//                menuSelect = 0;
//            }
//
//            if ((menuSelect <= 0) || (menuSelect >= 8)) {
//                System.out.println("Please enter a valid menu input value");
//            }
//
//            if (menuSelect == 1) {
//
//                while (menuSelect == 1) {
//
//                    System.out.println("Please enter number of horizontal lanes[min 1, max 3]: ");
//                    Catch = keyboard.next();
//
//                    try {
//                        H_street = Integer.parseInt(Catch);
//                    } catch (Exception e) {
//                    }
//                    if ((H_street > 0) && (H_street < 4)) {
//
//                        runcrossing.crossing.setPaused(true);
//                        runcrossing.carsCrossingFrame.setPaused(true);
//                        runcrossing.setCrossing(H_street, V_street);
//                        runcrossing.carsCrossingFrame.newLanes(runcrossing.crossing.getLanes());
//
//
//                        System.out.println("No. of Lanes" + runcrossing.crossing.getLanes().length);
//
//                        break;
//                    }
//                    System.out.println("Please enter a valid input value");
//                }
//            }
//
//            if (menuSelect == 2) {
//
//                while (menuSelect == 2) {
//
//                    System.out.println("Please enter number of vertical lanes[min 1, max 4]: ");
//                    Catch = keyboard.next();
//                    try {
//                        V_street = Integer.parseInt(Catch);
//                    } catch (Exception e) {
//                        V_street = 5;
//                    }
//                    if ((V_street > 0) && (V_street < 5)) {
//                        pressFive = false;
//                        break;
//                    }
//                    System.out.println("Please enter a valid input value");
//                }
//            }
//
//            if (menuSelect == 3) {
//
//                while (menuSelect == 3) {
//
//                    System.out.println("Please enter a value for the"
//                            + "Probability of a car entering "
//                            + "a horizontal street [min 0, max 1]: ");
//                    Catch = keyboard.next();
//                    try {
//                        chanceCarH_street = Double.parseDouble(Catch);
//                    } catch (Exception e) {
//                        chanceCarH_street = 2;
//                    }
//                    if ((chanceCarH_street > 0) && (chanceCarH_street < 1)) {
//                        pressFive = false;
//                        break;
//                    }
//                    System.out.println("Please enter a valid input value");
//                }
//            }
//
//            if (menuSelect == 4) {
//
//                while (menuSelect == 4) {
//
//                    System.out.println("Please enter a value for the"
//                            + "Probability of a car entering "
//                            + "a vertical street [min 0, max 1]: ");
//                    Catch = keyboard.next();
//                    try {
//                        chanceCarV_street = Double.parseDouble(Catch);
//                    } catch (Exception e) {
//                        chanceCarV_street = 2;
//                    }
//                    if ((chanceCarV_street > 0) && (chanceCarV_street < 1)) {
//                        pressFive = false;
//                        break;
//                    }
//                    System.out.println("Please enter a valid input value");
//                }
//            }


//        }
    }
}
